package com.cskaoyan.service.promotion;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.goods.MarketGoods;
import com.cskaoyan.bean.promotion.topic.*;
import com.cskaoyan.mapper.TopicMapper;
import com.cskaoyan.mapper.goodsMapper.MarketGoodsMapper;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: WD created on 2021-09-17 16:39
 * @Version:
 */

@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;


    @Override
    public ShowTopicListVO getList(BaseParam param, String title, String subtitle) {
        PageUtil.start(param);
        TopicExample topicExample = new TopicExample();
        topicExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        TopicExample.Criteria criteria = topicExample.createCriteria();
        if (StringUtil.isNotEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (StringUtil.isNotEmpty(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }

        criteria.andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        PageInfo<Topic> topicPageInfo = new PageInfo<>(topics);
        List<TopicVO> topicVOS = new ArrayList<>();
        for (Topic topic : topics) {

            topicVOS.add(new TopicVO(topic));

        }
        return new ShowTopicListVO(topicPageInfo, topicVOS);


    }

    @Override
    public GetTopicInfoVO getTopicInfo(Integer id) {
        Topic topic = topicMapper.selectByPrimaryKey(id);
        Integer[] goodsIds = topic.getGoods();
        List<GetTopicInfoInnerGoods> goodsList = new ArrayList<>();
        for (Integer goodsId : goodsIds) {
            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(goodsId);
            goodsList.add(new GetTopicInfoInnerGoods(marketGoods));
        }
        GetTopicInfoInnerTopic getTopicInfoInnerTopic = new GetTopicInfoInnerTopic(topic);

        return new GetTopicInfoVO(goodsList, getTopicInfoInnerTopic);

    }

    @Override
    public UpdateTopicVO updateTopic(UpdateTopicBO topicBO) {
        Topic topic = new Topic(topicBO);
        topicMapper.updateByPrimaryKeySelective(topic);
        return new UpdateTopicVO(topic);
    }

    @Override
    public CreateTopicVO createTopic(CreateTopicBO topicBO) {
        Topic topic = new Topic(topicBO);
        int i = topicMapper.insertSelective(topic);

        return new CreateTopicVO(topic);


    }

    @Override
    public Integer deleteTopicNotReal(Integer id) {
        Integer integer = topicMapper.deleteByIdNotReal(id);

        return integer;

    }

    @Override
    public Integer deleteTopicsByIdNotReal(List<Integer> ids) {
        Integer integer = 0;
        for (Integer id : ids) {
            integer += deleteTopicNotReal(id);
        }

        return integer;
    }


}
